package io.renren.com.qywt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.com.qywt.dao.AftersaleFinanceCollectDao;
import io.renren.com.qywt.dto.AftersaleFinanceCollectDTO;
import io.renren.com.qywt.entity.AftersaleFinanceCollectEntity;
import io.renren.com.qywt.service.*;
import io.renren.commons.mybatis.service.impl.CrudServiceImpl;

import io.renren.commons.security.context.TenantContext;
import io.renren.commons.security.user.SecurityUser;
import io.renren.commons.security.user.UserDetail;
import io.renren.commons.tools.utils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;

/**
 * 财务结算汇总表
 *
 * @author jiaohj 15110414840@139.com
 * @since 3.0 2022-08-10
 */
@Service
public class AftersaleFinanceCollectServiceImpl extends CrudServiceImpl<AftersaleFinanceCollectDao, AftersaleFinanceCollectEntity, AftersaleFinanceCollectDTO> implements AftersaleFinanceCollectService {
    @Autowired
    private AftersaleStorageFeeService storageFeeService;
    @Autowired
    private AftersaleReturnFeeService returnFeeService;
    @Autowired
    private AftersalePurchasereturnFeeService purchasereturnFeeService;
    @Autowired
    private AftersaleLocationFeeService locationFeeService;
    @Autowired
    private AftersaleEjectFeeService ejectFeeService;
    @Autowired
    private AftersaleCarriageFeeService carriageFeeService;

    @Override
    public QueryWrapper<AftersaleFinanceCollectEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<AftersaleFinanceCollectEntity> wrapper = new QueryWrapper<>();
        String collectDate = (String)params.get("collectDate");
        wrapper.eq(StringUtils.isNotBlank(collectDate),"date_format(collect_date, '%Y-%m-%d')",collectDate);
        wrapper.eq("tenant_code", TenantContext.getTenantCode(SecurityUser.getUser()));
        wrapper.lambda().orderByAsc(AftersaleFinanceCollectEntity::getCollectDate);
        return wrapper;
    }


    @Override
    public AftersaleFinanceCollectDTO getByBatchNo(String batchNo) {
        AftersaleFinanceCollectEntity entity = baseDao.selectOne(new QueryWrapper<AftersaleFinanceCollectEntity>().eq("batch_no", batchNo));
        return ConvertUtils.sourceToTarget(entity, AftersaleFinanceCollectDTO.class);
    }

    @Override
    public void deleteFeeByBatchNo(String batchNo, UserDetail userDetail) {
        locationFeeService.deleteByBatchNo(batchNo,userDetail);
        storageFeeService.deleteByBatchNo(batchNo,userDetail);
        returnFeeService.deleteByBatchNo(batchNo,userDetail);
        purchasereturnFeeService.deleteByBatchNo(batchNo,userDetail);
        ejectFeeService.deleteByBatchNo(batchNo,userDetail);
        carriageFeeService.deleteByBatchNo(batchNo,userDetail);
    }
}